/*
Template.tasksList.helpers({
    tasks: function () {
		if (Session.get("allUser")) {
            if (Session.get("hideCompleted")) {
              // If hide completed is checked, filter tasks
              return Tasks.find({checked: {$ne: true}}, {sort: {createdAt: -1}});
            } else {
              // Otherwise, return all of the tasks
              return Tasks.find({}, {sort: {createdAt: -1}});
            }
		} else {
			if (Session.get("hideCompleted")) {
              // If hide completed is checked, filter tasks
              return Tasks.find({checked: {$ne: true}, owner: Meteor.userId()}, {sort: {createdAt: -1}});
            } else {
              // Otherwise, return all of the tasks
              return Tasks.find({owner: Meteor.userId()}, {sort: {createdAt: -1}});
            }
		}
    },
    hideCompleted: function () {
      return Session.get("hideCompleted");
    },
    allUser: function () {
      return Session.get("allUser");
    },
	incompleteCount: function () {
		if (Session.get("allUser")) {
			return Tasks.find({checked: {$ne: true}}).count();
		} else {
			return Tasks.find({checked: {$ne: true}, owner: Meteor.userId()}).count();
		}
    },
    totalCount: function () {
    	if (Session.get("allUser")) {
			return Tasks.find({}).count();
		} else {
			return Tasks.find({owner: Meteor.userId()}).count();
		}
    },
    pages: function () {
    	var pageSize = 5;
    	var totalCount = Tasks.find({owner: Meteor.userId()}).count();
    	var pageCount = parseInt((totalCount+pageSize-1)/pageSize);
    	return pageCount;
    }
});*/
myMd5Demo = new MysqlSubscription('findStrByMd5', 'a');
Template.tasksList.helpers({
  getStrByMd5: function(md5_str){
    var data = myMd5Demo.reactive();
    return data.length >= 1 && data[0].str;
  }
});
  
console.log('tasksList.js begin');
Template.tasksList.events({
    "submit .new-task": function (event) {
      // This function is called when the new task form is submitted
  
      var text = event.target.text.value;
  
      Meteor.call("addTask", text);
  
      // Clear form
      event.target.text.value = "";
  
      // Prevent default form submit
      return false;
    },
    // Add to Template.body.events
    "change .hide-completed input": function (event) {
      Session.set("hideCompleted", event.target.checked);
    },
    "change .all-user input": function (event) {
      Session.set("allUser", event.target.checked);
    },
    "click #getTimeBtn": function(e) {
      // try {
      //   // 其实这种方式不可“跨域”调用
      //   var method = "POST";
      //   var url = "http://192.168.56.1:6202/code_igniter/index.php/Welcome/getTime";
      //   var data = HTTP.call(method, url, function(err, result){
      //     console.log(err);
      //     console.log(result);
      //   });
      // } catch(ex) {
      //   console.log(ex);
      // }
      // return;

      // syscall
      Meteor.call("getTime", function(err, result){
        console.log(err);
        console.log(result);
        if (!err) {
          var json = JSON.parse(result.content);
          alert(json.data);
        }
      });
    }
});
console.log('tasksList.js end');
